﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Reflection;
using System.IO;

using mbine.msgc.model;
using mbine.msgc.tool;
using mbine.msgc.controls;
using System.Threading;

namespace mbine.msgc.forms
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private CClient Client;

        public MainWindow()
        {
            InitializeComponent();

            Init();

            InitControl();

            ShowLogin();
        }

        void Init()
        {
            CInfo.Instance.Info = CXml.Parser(Properties.Resources.serverinfo);
            Client = new CClient();
            Client.EventLogin += new EventHandler(Client_EventLogin);
            Client.EventLogout += new EventHandler(Client_EventLogout);
            Client.EventChatThreadAdd += new EventHandler(Client_EventChatThreadAdd);
            this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
        }

        void Client_EventChatThreadAdd(object sender, EventArgs e)
        {
            uCChatList.Add(Client.RelayMessage.Members, Client.RelayMessage.roomkey);
            uCChatList.Update();
        }

        //로그아웃 시에 화면 전환
        void Client_EventLogout(object sender, EventArgs e)
        {
            Dispatcher.Invoke(new ThreadStart(() =>
            {
                ShowLogin();
            }));
        }

        //로그인 시에 화면 전환
        void Client_EventLogin(object sender, EventArgs e)
        {
            Dispatcher.Invoke(new ThreadStart(() =>
            {
                ShowLogged();
            }));
            uCRoster.ReloadRoster();
        }

        //로그인 실패
        void uCLogin_EventLoginFail(object sender, EventArgs e)
        {
            Dispatcher.Invoke(new ThreadStart(() =>
            {
                ShowLogin();
            }));
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Client.Close();
        }

        void InitControl()
        {
            this.Width = 600;
            this.Height = 600;

            uCLogin.EventLoginFail += new EventHandler(uCLogin_EventLoginFail);
            uCRoster.EventChatStart += new EventHandler(uCRoster_EventChatStart);

            uCChatList.EventSetThread += new EventHandler(uCChatList_EventSetThread);
            uCChatList.EventThreadChanged += new EventHandler(uCChatList_EventThreadChanged);
            uCChat.EventChatSend += new EventHandler(uCChat_EventChatSend);

            this.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.AllowsTransparency = true;
            this.BorderThickness = new Thickness(3);
            this.BorderBrush = new SolidColorBrush(Colors.Black);

            CObserveRoster.Instance.Add(uCRoster);
            CObserveRoster.Instance.Add(uCChatList);
        }

        void uCChatList_EventThreadChanged(object sender, EventArgs e)
        {
            uCChat.Clear();
            uCChat.LoadMessages(uCChatList.NowThread);
        }

        void uCChat_EventChatSend(object sender, EventArgs e)
        {
            uCChatList.NowThread.Add(uCChat.SendData);
            uCChat.AddMessage(uCChatList.NowThread.Messages.Last());
        }

        void uCChatList_EventSetThread(object sender, EventArgs e)
        {
            if (Client.RelayMessage != null)
            {
                uCChatList.NowThread.Add(Client.RelayMessage);
                uCChat.AddMessage(uCChatList.NowThread.Messages.Last());
                Client.RelayMessage = null;
            }
        }

        void uCRoster_EventChatStart(object sender, EventArgs e)
        {
            bool newWindow = false;
            //쳇을 새로운 창으로 할 경우...
            if (newWindow)
            {
                CChatContainer.Instance.Add(((UCRoster)sender).SelectedUsers);
            }
            else
            {
                uCChatList.Add(((UCRoster)sender).SelectedUsers, "");
            }
        }

        void ShowLogin()
        {
            MainGrid.ColumnDefinitions[0].Width = new GridLength(200, GridUnitType.Star);
            MainGrid.ColumnDefinitions[1].Width = new GridLength(0, GridUnitType.Pixel);// GridLength.Auto;
            MainGrid.ColumnDefinitions[2].Width = new GridLength(0, GridUnitType.Pixel);// GridLength.Auto;

            uCRoster.Visibility = System.Windows.Visibility.Collapsed;
            uCLogin.Visibility = System.Windows.Visibility.Visible;
            gridSplitter1.Visibility = System.Windows.Visibility.Collapsed;
            gridSplitter2.Visibility = System.Windows.Visibility.Collapsed;
        }

        void ShowLogged()
        {
            MainGrid.ColumnDefinitions[0].Width = new GridLength(200, GridUnitType.Star);
            MainGrid.ColumnDefinitions[1].Width = new GridLength(200, GridUnitType.Star);
            MainGrid.ColumnDefinitions[2].Width = new GridLength(200, GridUnitType.Star);

            uCRoster.Visibility = System.Windows.Visibility.Visible;
            uCLogin.Visibility = System.Windows.Visibility.Collapsed;
            gridSplitter1.Visibility = System.Windows.Visibility.Visible;
            gridSplitter2.Visibility = System.Windows.Visibility.Visible;
        }

        private void Top_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
